import {
  lockSysUser
} from '../api/member';

function datetimeFormat(time) {
  var date = new Date(time);
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function datetimeFormatTZ(time) {
  var date = new Date(time);
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('-') + 'T' + [hour, minute, second].map(formatNumber).join(':')+'Z'
}

function datetime(time) {
  var date = new Date(time);
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}


const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 创建全局唯一标识符
const getUUID = () => {
  var d = new Date().getTime()
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = (d + Math.random() * 16) % 16 | 0
    d = Math.floor(d / 16)
    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16)
  })
  return uuid
}

const getUrlParameter = (name) => {
  var url = window.location.href
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  var r = url.slice(url.indexOf('?') + 1).match(reg)
  if (r != null) {
    return decodeURIComponent(r[2])
  }
  return null
}
const thumbnail = (url, w, h) => {
  if (isNull(url)) {
    return "";
  }
  if (url.indexOf('?x-oss-') != -1) {
    url = url.substring(0, url.indexOf('?x-oss-'));
  }
  h = Math.round(h);
  w = Math.round(w);
  if (url.substring(0, 11) == "http://cdnx") {
    return url + "?x-oss-process=image/resize,m_fill,w_" + w + ",h_" + h + "/quality,q_90";
  } else if (url.substring(0, 10) == "http://cdn") {
    return url + "@" + w + "w_" + h + "h_1e_1c_100Q";
  } else {
    return url;
  }
}

const proxyImage = (host, url) => {
  return host + "/zxing/proxyImage?url=" + encodeURIComponent(url);

}

const thumbnail_cover = (url, w) => {
  if (w > 1080) {
    w = 1080
  }
  w = Math.round(w);
  if (url.substring(0, 11) == 'http://cdnx') {
    return url + '?x-oss-process=image/resize,w_' + w + '/quality,q_90';
  } else
    if (url.substring(0, 10) == 'http://cdn') {
      return url + '@' + w + 'w_1e_1c_100Q';
    } else {
      return url;
    }
}


const token = (data) => {
  if (data == null) {
    console.log(data)
    wx.navigateTo({
      url: '/pages/login/index',
    })
    return
  }
}

// **
//  * 判断空值，包括{ } 和[]，空为true，否则为false
//   * /
const isNull = function (value) {
  if (value == null || value == undefined) return true
  if (isString(value)) {
    if (value.trim().length == 0) return true
  } else if (isArray(value)) {
    if (value.length == 0) return true
  } else if (isObject(value)) {
    for (let name in value) return false
    return true
  }
  return false;
}
/**
 * 判断是否为字符串类型，是为true，否则为false
 */
const isString = function (value) {
  return value != null && value != undefined && value.constructor == String
}
/**
 * 判断是否为数字类型，是为true，否则为false
 */
const isNumber = function (value) {
  return value != null && value != undefined && value.constructor == Number
}
/**
 * 判断是否为布尔类型，是为true，否则为false
 */
const isBoolean = function (value) {
  return value != null && value != undefined && value.constructor == Boolean
}
/**
 * 判断是否为数组类型，是为true，否则为false
 */
const isArray = function (value) {
  return value != null && value != undefined && value.constructor == Array
}
/**
 * 判断是否为对象类型，是为true，否则为false
 */
const isObject = function (value) {
  return value != null && value != undefined && value.constructor == Object
}
/**
 * 判断是否为方法类型，是为true，否则为false
 */
const isFunction = function (value) {
  return value != null && value != undefined && value.constructor == Function
}
/**
 * 判断是否手机号是否合法，是为true，否则为false
 */
const isPhone = function (value) {
  if (!/^1(3|4|5|7|8)\d{9}$/.test(value)) {
      return false
  } else {
      return true
  }
}

const forwarding = function (value, token) {
  var app = getApp();
  console.log(token)
  if (!isNull(value.xuid)) {
    app.globalData.xuid = value.xuid;
    console.log('查看是否有xuid2')
    console.log(app.globalData.xuid)
    if (token == null) {
      wx.navigateTo({
        url: '/pages/login/index?cardType=true'
      })
    } else {
      lockSysUserClick(app.globalData.xuid)
    }
  }
}

const lockSysUserClick = function (xuid) {
  var app = getApp();
  lockSysUser({ mchId: app.globalData.mchId, xuid: xuid, shopId: app.globalData.shopId }).then(res => {
    console.log(res)
  })
}
function throttle(fn, interval) {
  var enterTime = 0;//触发的时间
  var gapTime = interval || 300;//间隔时间，如果interval不传，则默认300ms
  return function () {
    var context = this;
    var backTime = new Date();//第一次函数return即触发的时间
    if (backTime - enterTime > gapTime) {
      fn.call(context, arguments);
      enterTime = backTime;//赋值给第一次触发的时间，这样就保存了第二次触发的时间
    }
  };
}
function debounce(fn, interval) {
  var timer;
  var gapTime = interval || 1000;//间隔时间，如果interval不传，则默认1000ms
  return function() {
    clearTimeout(timer);
    var context = this;
    var args = arguments;//保存此处的arguments，因为setTimeout是全局的，arguments不是防抖函数需要的。
    timer = setTimeout(function() {
      fn.call(context,args);
    }, gapTime);
  };
}


module.exports = {
  datetimeFormatTZ:datetimeFormatTZ,
  debounce:debounce,
  throttle:throttle,
  datetimeFormat: datetimeFormat,
  datetime: datetime,
  getUUID: getUUID,
  isNull: isNull,
  isString: isString,
  isArray: isArray,
  isNumber: isNumber,
  isBoolean: isBoolean,
  isObject: isObject,
  isPhone: isPhone,
  getUrlParameter: getUrlParameter,
  thumbnail: thumbnail,
  thumbnail_cover: thumbnail_cover,
  token: token,
  proxyImage: proxyImage,
  forwarding: forwarding,
  lockSysUserClick: lockSysUserClick
}